/*     */ package com.java.diagnostics.visualizer.impl.templates;
/*     */ 
/*     */ import com.java.diagnostics.visualizer.impl.GCAndMemoryVisualizerImplPreferenceInitalizer;
/*     */ import com.java.diagnostics.visualizer.impl.factory.LogFactory;
/*     */ import com.java.diagnostics.visualizer.impl.preferences.GCAndMemoryVisualizerPreferenceInitializer;
/*     */ import com.java.diagnostics.visualizer.impl.preferences.SmartPreferences;
/*     */ import com.java.diagnostics.visualizer.impl.util.Messages;
/*     */ import java.util.Iterator;
/*     */ import java.util.SortedSet;
/*     */ import java.util.TreeSet;
/*     */ import java.util.logging.Logger;
/*     */ import org.eclipse.core.runtime.preferences.ConfigurationScope;
/*     */ import org.osgi.service.prefs.Preferences;
/*     */ 
/*     */ public class TemplateList
/*     */ {
/*  27 */   private static final String[][] DEFAULT_TEMPLATES = { 
/*  28 */     { "Memory", "TemplateList.heap.title", "TemplateList.heap.description", "platform:          org.eclipse.ui/icons/full/obj16/keygroups_obj.gif" }, 
/*  29 */     { "Performance", "TemplateList.pauses.title", "TemplateList.pauses.description", "platform:          org.eclipse.ui/icons/full/progress/waiting.gif" }, 
/*  30 */     { "Native Memory", "TemplateList.native.memory.title", "TemplateList.native.memory.description", "platform:          org.eclipse.ui/icons/full/obj16/keygroups_obj.gif" }, 
/*  31 */     { "Compaction Pauses", "TemplateList.compaction.pauses.title", "TemplateList.compaction.pauses.description", "platform:          org.eclipse.ui/icons/full/progress/waiting.gif" }, 
/*  32 */     { "Object Sizes", "TemplateList.object.sizes.title", "TemplateList.object.sizes.description", "platform:          org.eclipse.ui/icons/full/obj16/keygroups_obj.gif" }, 
/*  33 */     { "Fragmentation", "TemplateList.fragmentation.title", "TemplateList.fragmentation.description", "platform:          org.eclipse.ui/icons/full/obj16/keygroups_obj.gif" }, 
/*  34 */     { "Unpaused Time", "TemplateList.unpaused.time.title", "TemplateList.unpaused.time.description", "platform:          org.eclipse.ui/icons/full/progress/waiting.gif" }, 
/*  35 */     { "Generational Heap", "TemplateList.generational.heap.title", "TemplateList.generational.heap.description", "platform:          org.eclipse.ui/icons/full/obj16/keygroups_obj.gif" }, 
/*  36 */     { "LOA and SOA Sizes", "TemplateList.loa.vs.soa.title", "TemplateList.loa.vs.soa.description", "platform:          org.eclipse.ui/icons/full/obj16/keygroups_obj.gif" }, 
/*  37 */     { "NUMA", "TemplateList.numa.title", "TemplateList.numa.description", "platform:          org.eclipse.ui/icons/full/obj16/keygroups_obj.gif" } };
/*     */ 
/*  40 */   private static final Logger TRACE = LogFactory.getTrace(TemplateList.class);
/*     */   private static final String CLOSE_BRACE = "}";
/*     */   private static final String OPEN_BRACE = "{";
/*     */   private static final String COMMA = ", ";
/*     */   private SortedSet<Template> set;
/*     */   private SmartPreferences preferences;
/*  51 */   private static final String PREFERENCE_NAME = TemplateList.class + 
/*  51 */     "TemplateList";
/*     */ 
/*     */   public TemplateList()
/*     */   {
/*  58 */     this.preferences = 
/*  59 */       GCAndMemoryVisualizerImplPreferenceInitalizer.getInstance().getPreferences(new ConfigurationScope(), 
/*  60 */       new ConfigurationScope());
/*  61 */     this.set = ((SortedSet)this.preferences.getObject(PREFERENCE_NAME));
/*  62 */     if (this.set == null) {
/*  63 */       TRACE.warning(Messages.getString("TemplateList.10"));
/*  64 */       this.set = getDefaultTemplates();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void resetToDefaults() {
/*  69 */     this.preferences.setToDefault(PREFERENCE_NAME);
/*  70 */     this.set = ((SortedSet)this.preferences.getObject(PREFERENCE_NAME));
/*  71 */     if (this.set == null)
/*  72 */       this.set = getDefaultTemplates();
/*     */   }
/*     */ 
/*     */   public Template[] getTemplates(int targetCount)
/*     */   {
/*  77 */     Iterator it = this.set.iterator();
/*  78 */     int count = Math.min(this.set.size(), targetCount);
/*  79 */     Template[] templates = new Template[count];
/*  80 */     for (int i = 0; i < count; i++) {
/*  81 */       if (it.hasNext()) {
/*  82 */         templates[i] = ((Template)it.next());
/*     */       }
/*     */     }
/*  85 */     return templates;
/*     */   }
/*     */ 
/*     */   public void add(Template template) {
/*  89 */     this.set.add(template);
/*  90 */     this.preferences.setValue(PREFERENCE_NAME, this.set);
/*     */   }
/*     */ 
/*     */   public Template[] getTemplates() {
/*  94 */     return getTemplates(this.set.size());
/*     */   }
/*     */ 
/*     */   public boolean contains(Object o) {
/*  98 */     return this.set.contains(o);
/*     */   }
/*     */ 
/*     */   public String toString() {
/* 102 */     StringBuffer buf = new StringBuffer();
/* 103 */     buf.append("{");
/* 104 */     if (this.set != null) {
/* 105 */       Iterator it = this.set.iterator();
/* 106 */       while (it.hasNext()) {
/* 107 */         buf.append(it.next());
/* 108 */         if (it.hasNext()) {
/* 109 */           buf.append(", ");
/*     */         }
/*     */       }
/*     */     }
/* 113 */     buf.append("}");
/* 114 */     return buf.toString();
/*     */   }
/*     */ 
/*     */   public static void initializeDefaults(Preferences preferences)
/*     */   {
/* 119 */     preferences.put(PREFERENCE_NAME, SmartPreferences.serialize(getDefaultTemplates()));
/*     */   }
/*     */ 
/*     */   public static TreeSet getDefaultTemplates() {
/* 123 */     TreeSet set = new TreeSet();
/*     */ 
/* 125 */     for (int i = 0; i < DEFAULT_TEMPLATES.length; i++) {
/* 126 */       Template template = new ResourceTemplate(DEFAULT_TEMPLATES[i][1], DEFAULT_TEMPLATES[i][0]);
/* 127 */       template.setDescription(DEFAULT_TEMPLATES[i][2]);
/*     */ 
/* 129 */       template.setIconUrl(DEFAULT_TEMPLATES[i][3]);
/* 130 */       set.add(template);
/*     */     }
/* 132 */     return set;
/*     */   }
/*     */ }

